import { getLeisurelyList, getLeisurelyInfo, creatIdleOrder, wxPay, issuerIdle,
  myReleaseIdleOrder, mySellIdleOrder, myBuyIdleOrder, idleOrderDetails,
  idleOffshelve, idleDelivery, idleReceipt, getLeisurelyImgs, getPayerScen,
  idleRefreshTime, submitRefund, returnDetails, cancelRefund, agreesRefund,
  submitRefused, refundDelivery
 } from "../apis/index.js";
import { User }  from "./user.js";
let user = new User()

class Leisurely {
	
	current = {}
	order
	
	
	//代理模式
	constructor() {
		// 已经实例化直接返回实例对象
		if (typeof Leisurely.instance === 'object') {
			return Leisurely.instance;
		}

		// 缓存
		Leisurely.instance = this;
		return this;

	}
		
    static async getLeisurelyList(pagenum) {
		return getLeisurelyList({
			pagenum
		})
    }
	
    static async getLeisurelyInfo(productId) {
	   return getLeisurelyInfo({
			productId
	   })
    }
	
	static async issuerIdle(idle) {
	   return issuerIdle({
			...idle,
			userName: user.username,
	   })
	}
	
	static async submitRefund(idle) {
	   return submitRefund({
			...idle,
			userName: user.username,
	   })
	}
	
	
	static async submitRefused(idle) {
	   return submitRefused({
			...idle,
			userName: user.username,
	   })
	}
	
	static async returnDetails(returnsNumber) {
	   return returnDetails({
			returnsNumber,
			userName: user.username,
	   })
	}
	
	static async cancelRefund(returnsNumber) {
	   return cancelRefund({
			returnsNumber,
			userName: user.username,
	   })
	}
	
	static async agreesRefund(returnsNumber) {
	   return agreesRefund({
			returnsNumber,
			userName: user.username,
	   })
	}
	
	static async refundDelivery(orderId, couriercompany, courierNumber) {
	   return refundDelivery({
			orderId,
			userName: user.username,
			couriercompany,
			courierNumber
	   })
	}
	
	static async idleOffshelve(productId) {
	   return idleOffshelve({
			productId,
			userName: user.username,
	   })
	}
	
	static async idleDelivery(orderId, couriercompany, courierNumber) {
	   return idleDelivery({
			orderId,
			userName: user.username,
			couriercompany,
			courierNumber
	   })
	}
	
	
	
	static async idleReceipt(orderId) {
	   return idleReceipt({
			orderId,
			userName: user.username,
	   })
	} 
	
	static async getLeisurelyImgs(productId) {
	   return getLeisurelyImgs({
			productId
	   })
	} 
	

	
	currentLeisurely(current) {
		if(!user.username) {
			return '您还未登录，先去登录'
		}
		if(current.userName == user.username) {
			return '不能购买自己发布的闲本'
		}
		this.current = current
		return false
	}
	
	async createOrder(addressId, productId, isVerify, version, productStock) {
		return creatIdleOrder({
			addressId,
			userName: user.username,
			productId,
			isVerify: isVerify?1:0,
			version,
			productStock
		})
	}

	async wxPay({orderId , productId, orderAmount}) {
		let buyerOpenid =  await user.getTokenFromServer();
		return wxPay({
			orderId,
			userName: user.username,
			productId,
			orderAmount,
			buyerOpenid
		})
	}
	
	static async idleRefreshTime(productId) {
		return idleRefreshTime({
			productId
		})
	}
	

	static async myReleaseIdleOrder(pagenum) {
		return myReleaseIdleOrder({
			userName: user.username,
			pagenum,
		})
	}
	
	static async mySellIdleOrder(pagenum) {
		return mySellIdleOrder({
			userName: user.username,
			pagenum,
		})
	}
	
	static async myBuyIdleOrder(pagenum) {
		return myBuyIdleOrder({
			userName: user.username,
			pagenum,
		})
	}
	
	static async idleOrderDetails(orderId, identity) {
		return idleOrderDetails({
			userName: user.username,
			orderId,
			identity
		})
	}
	
}

export {
    Leisurely
}